<template>
  <div class="comment">
    <div class="header border-bottom">
      <span class="line"></span>
      用户评论
    </div>
    <div class="item border-bottom" v-for="(item,index) in list" :key="item.id">
      <div class="user">
        <div class="left">
          <span
            class="stars iconfont"
            v-for="(star,starIndex) in item.stars"
            :key="starIndex"
          >&#xe683;</span>
        </div>
        <div class="right">
          <span class="username">{{item.user}}</span>
          <span class="time">{{item.time}}</span>
        </div>
      </div>
      <div class="text">
        <p>{{item.desc}}</p>
        <span
          class="showMore iconfont up"
          v-show="item.desc.length>100"
          @click="showMoreClick(index)"
        >&#xe60c;</span>
        <span
          class="showMore iconfont down"
          v-show="item.desc.length<=100"
          @click="showMoreClick(index)"
        >&#xe779;</span>
      </div>
      <ul class="img-list">
        <li v-for="(item,imgIndex) in item.imgUrl" :key="imgIndex" @click="showGalleryClick(index)">
          <img :src="item" alt />
        </li>
        <span class="img-list-num">共{{item.imgUrl.length}}张</span>
      </ul>
      <comment-gallery
        :galleryImg="item.imgUrl"
        v-show="index==galleryIndex"
        @hideGallery="hideGallery"
      ></comment-gallery>
    </div>
  </div>
</template>

<script>
import CommentGallery from "@/page/common/gallery/Gallery";
export default {
  name: "DetailComment",
  components: {
    CommentGallery
  },
  data() {
    return {
      showMore: false,
      showGallery: false,
      list: [],
      deepCloneList: [],
      galleryIndex: null
    };
  },
  props: {
    commentList: Array
  },
  watch: {
    commentList(newVal) {
      // 这里深拷贝，保存一份原始数据
      this.deepCloneList = JSON.parse(JSON.stringify(newVal));
      this.list = newVal;
      this.list.forEach(item => {
        if (item.desc.length > 100) {
          item.desc = item.desc.substring(0, 100);
        }
      });
      return this.list;
    }
  },
  methods: {
    showMoreClick(index) {
      if (this.list[index].desc.length <= 100) {
        this.list[index].desc = this.deepCloneList[index].desc;
      } else {
        this.list[index].desc = this.deepCloneList[index].desc.substring(
          0,
          100
        );
      }
    },
    showGalleryClick(index) {
      this.galleryIndex = index;
    },
    hideGallery() {
      this.galleryIndex = null;
    }
  },
  mounted() {}
};
</script>

<style lang="stylus" scoped>
@import '~@/assets/style/variables'

.comment
  margin-top: 0.2rem
  background: #fff
  .header
    height: $headerHeight
    line-height: $headerHeight
    font-size: 0.3rem
    padding-left: 0.2rem
    .line
      display: inline-block
      width: 0.06rem
      height: 0.25rem
      background: $bgColor
      margin-right: 0.05rem
  .item
    padding: 0.2rem
    .user
      display: flex
      flex-direction: row
      justify-content: space-between
      .stars
        font-size: 0.28rem
        color: #ffb436
    .text
      p
        font-size: 0.26rem
        color: #616161
        padding-bottom: 0.1rem
      .showMore
        text-align: center
        display: block
        padding: 0.2rem 0
    .img-list
      display: flex
      flex-direction: row
      flex-wrap: wrap
      position: relative
      li:nth-child(3n)
        margin-right: 0
      li
        margin-right: 0.13rem
        margin-bottom: 0.1rem
        img
          width: 2.28rem
          height: 1.68rem
      .img-list-num
        position: absolute
        right: 0
        bottom: 0.4rem
        width: 1rem
        height: 0.4rem
        line-height: 0.4rem
        font-size: 0.24rem
        color: #fff
        background: rgba(0, 0, 0, 0.5)
        text-align: center
        border-top-left-radius: 0.3rem
        border-bottom-left-radius: 0.3rem
</style>